﻿using BaseUi.Models;
using BaseUi.ViewModels;
using Ping9719.WpfEx;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BaseUi 
{ 
    /// <summary>
    /// BoxsView.xaml 的交互逻辑
    /// </summary>
    public partial class BoxsView : UserControlBase
    {
        BoxsViewModel viewModel = new BoxsViewModel();
        public BoxsView()
        {
            InitializeComponent();
            this.DataContext = viewModel;
        }

        private void loadfir(object sender, EventArgs e)
        {
            //if (this.IsInDesignMode)
            //    return;

            viewModel.DeviceData1 = LocalStatic.Test[0];
            viewModel.DeviceData2 = LocalStatic.Test[1];
            viewModel.DeviceData3 = LocalStatic.Test[2];
            viewModel.DeviceData4 = LocalStatic.Test[3];
        }

        private void textchange(object sender, TextChangedEventArgs e)
        {
            var nam = (sender as TextBox);
            nam.ScrollToEnd();
        }
        /// <summary>
        /// 按压气缸与顶升气缸是否可以使用
        /// </summary>
        /// <returns></returns>
        private string anysafe()
        {
            if (!MainWindow2.viewModel.ischengg)
            {
                return "转盘状态通信失败，"+ MainWindow2.viewModel.errtext;
            }
            string dian = MainWindow2.viewModel.msg[0] + " " + MainWindow2.viewModel.msg[1] + " ";
            if (dian == "00 01 ")
            {
                return "OK";
            }
            else if (dian == "01 00 ")
            {

                return "OK";
            }
            else if (dian == "00 00 ")
            {

                return "转盘状态异常,未到位";
            }else
            {
                return "转盘状态异常。两边同时到位";
            }
        }
        private string sfsafe()
        {
            if (!MainWindow2.viewModel.ischengg)
            {

                return "状态检测通信错误" + MainWindow2.viewModel.errtext;
            }
            if (MainWindow2.viewModel.msg[2] != "01")
            {
                return "左顶升1-1顶断开未检测到位" + MainWindow2.viewModel.msg[2];
            }

            if (MainWindow2.viewModel.msg[3] != "00")
            {
                return "左顶升1-2顶闭合异常" + MainWindow2.viewModel.msg[3];
            }
            if (MainWindow2.viewModel.msg[4] != "01")
            {
                return "右顶升2-1顶断开未检测到位" + MainWindow2.viewModel.msg[4];
            }
            if (MainWindow2.viewModel.msg[5] != "00")
            {
                return "右顶升2-2顶闭合异常" + MainWindow2.viewModel.msg[5];
            }

            if (MainWindow2.viewModel.msg[6] != "01")
            {
                return "开关箱报警";
            }
            if (MainWindow2.viewModel.msg[8] != "00")
            {
                return "急停按钮报警";
            }
            if (MainWindow2.viewModel.msg[9] != "00")
            {
                //return "光栅报警";
            }
            if (MainWindow2.viewModel.msg[10] != "01")
            {
                return "右按压气缸未上升";
            }
            if (MainWindow2.viewModel.msg[11] != "00")
            {
                return "右按压气缸下降";
            }
            if (MainWindow2.viewModel.msg[12] != "01")
            {
                return "左按压气缸未上升";
            }
            if (MainWindow2.viewModel.msg[13] != "00")
            {
                return "左按压气缸下降";
            }
            return "OK";
        }
        private void clear(object sender, RoutedEventArgs e)
        {
            textBoxInfo.Clear();
        }

        /// <summary>
        ///反向旋转
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xz1(object sender, RoutedEventArgs e)
        {
            try
            {
                string retul = sfsafe();
                if (retul != "OK")
                {
                    textBoxInfo.AppendText($"{retul}\r\n");
                    return;
                }
                var aaa = LocalStatic.Electric.Readsentrelay(500, PortModel.Rotate1, PortModel.Rotate2, LocalFile.Config.Address);
                if (aaa.IsSucceed)
                {
                    textBoxInfo.AppendText($"反向旋转成功\r\n");
                    HomeView.viewModel.way = 0;
                }
                else
                {
                    textBoxInfo.AppendText($"通信失败：{aaa.Err}\r\n");
                }
            }
            catch (Exception ex)
            {
                textBoxInfo.AppendText($"错误：{ex.Message}\r\n");
            }
        }

        /// <summary>
        ///正向旋转
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xz2(object sender, RoutedEventArgs e)
        {
            try
            {
                string retul = sfsafe();
                if (retul != "OK")
                {
                    textBoxInfo.AppendText($"{retul}\r\n");
                    return;
                }
                var aaa = LocalStatic.Electric.Readsentrelay(500, PortModel.Rotate2, PortModel.Rotate1, LocalFile.Config.Address);
                if (aaa.IsSucceed)
                {
                    textBoxInfo.AppendText($"正向旋转成功\r\n");
                    HomeView.viewModel.way = 0;
                }
                else
                {
                    textBoxInfo.AppendText($"通信失败：{aaa.Err}\r\n");
                }
            }
            catch (Exception ex)
            {
                textBoxInfo.AppendText($"错误：{ex.Message}\r\n");
            }
        }
        /// <summary>
        /// 将气缸顶升
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ds(object sender, RoutedEventArgs e)
        {
            try
            {
                string retul = anysafe();
                if (retul != "OK")
                {
                    textBoxInfo.AppendText($"{retul}\r\n");
                    return;
                }
                var aaa = LocalStatic.Electric.Readsentrelay(500, PortModel.Jack, new List<int>(), LocalFile.Config.Address);
                if (aaa.IsSucceed)
                {
                    textBoxInfo.AppendText($"顶升气缸成功顶升\r\n");
                }
                else
                {
                    textBoxInfo.AppendText($"失败：{aaa.Err}\r\n");
                }
            }
            catch (Exception ex)
            {
                textBoxInfo.AppendText($"错误：{ex.Message}\r\n");
            }
        }
        /// <summary>
        /// 将气缸顶升断电
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dsdk(object sender, RoutedEventArgs e)
        {
            try
            {
                string retul = anysafe();
                if (retul != "OK")
                {
                    textBoxInfo.AppendText($"{retul}\r\n");
                    return;
                }
                var aaa = LocalStatic.Electric.Readsentrelay(500, new List<int>(), PortModel.Jack, LocalFile.Config.Address);
                if (aaa.IsSucceed)
                {
                    textBoxInfo.AppendText($"顶升气缸成功下降\r\n");
                }
                else
                {
                    textBoxInfo.AppendText($"失败：{aaa.Err}\r\n");
                }
            }
            catch (Exception ex)
            {
                textBoxInfo.AppendText($"错误：{ex.Message}\r\n");
            }
        }
        /// <summary>
        /// 将气缸按压断电
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void aydk(object sender, RoutedEventArgs e)
        {
            try
            {
                string retul = anysafe();
                if (retul != "OK")
                {
                    textBoxInfo.AppendText($"{retul}\r\n");
                    return;
                }
                var aaa = LocalStatic.Electric.Readsentrelay(500, new List<int>(), PortModel.Suppress, LocalFile.Config.Address);
                if (aaa.IsSucceed)
                {
                    textBoxInfo.AppendText($"按压气缸成功断开\r\n");
                }
                else
                {
                    textBoxInfo.AppendText($"失败：{aaa.Err}\r\n");
                }
            }
            catch (Exception ex)
            {
                textBoxInfo.AppendText($"错误：{ex.Message}\r\n");
            }
        }


        /// <summary>
        /// 将气缸按压
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void ay(object sender, RoutedEventArgs e)
        {
            try
            {
                string retul = anysafe();
                if (retul != "OK")
                {
                    textBoxInfo.AppendText($"{retul}\r\n");
                    return;
                }
                var aaa = LocalStatic.Electric.Readsentrelay(500, PortModel.Suppress, new List<int>(), LocalFile.Config.Address);
                if (aaa.IsSucceed)
                {
                    textBoxInfo.AppendText($"按压气缸成功按压\r\n");
                }
                else
                {
                    textBoxInfo.AppendText($"失败：{aaa.Err}\r\n");
                }
            }
            catch (Exception ex)
            {
                textBoxInfo.AppendText($"错误：{ex.Message}\r\n");
            }
        }
    }
}
